﻿#include <iostream>
using namespace std;

//#include <map>
//#include <string>
#include <vector>
//#include <algorithm>
//#include <cmath>

//处理用时最长的那个任务的员工
//https://leetcode.cn/problems/the-employee-that-worked-on-the-longest-task/
//暴力破解
class Solution {
public:
    int hardestWorker(int n, vector<vector<int>>& logs) {
        vector<int> v;
        v.push_back(logs[0][1]);
        for (int i = 1; i < logs.size(); ++i) //记录差值
        {
            v.push_back(logs[i][1] - logs[i - 1][1]);
        }
        int max = 0;
        vector<int> vv;
        for (const auto& x : v) //找时间最大值
        {
            if (max < x) max = x;
        }
        for (int i = 0; i < v.size(); ++i) //存入最大值和最大值相同时间的员工下标
        {
            if (v[i] == max) vv.push_back(i); //计算时差时下标从1开始
        }
        int min = 99999999;
        for (int i = 0; i < vv.size(); ++i)
        {
            if (logs[vv[i]][0] < min) min = logs[vv[i]][0];
        }
        return min;
    }
};
//优化暴力破解
class Solution {
public:
    int hardestWorker(int n, vector<vector<int>>& logs) {
        int max = logs[0][1];
        int min = 99999999;
        for (int i = 1; i < logs.size(); ++i) //记录差值
        {
            if ((logs[i][1] - logs[i - 1][1]) >= max)
            {
                max = (logs[i][1] - logs[i - 1][1]);
            }
        }
        if (logs[0][1] == max && min > logs[0][0]) min = logs[0][0];
        for (int i = 1; i < logs.size(); ++i)
        {
            if ((logs[i][1] - logs[i - 1][1]) == max && min > logs[i][0]) min = logs[i][0];
        }
        return min;
    }
};

